//
//  SuspensionView.m
//  OperationPro
//
//  Created by yaxue wang on 16/9/28.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "SuspensionView.h"

@interface SuspensionView()
@property (nonatomic, strong) UIView *boxView;
@property (nonatomic, strong) NSArray *btns;

@property (nonatomic, strong) void(^action)(SuspensionView *, NSInteger);
@end
@implementation SuspensionView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+(SuspensionView *) showInputBoxbtns:(NSArray *)btns action:(void(^)(SuspensionView *boxView, NSInteger index))action
{
    SuspensionView *boxView = [[SuspensionView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight)];
    boxView.action = action;
    boxView.btns = btns;
    [boxView showBox];
    return boxView;
}
- (void) showBox{
    [self setBackgroundColor:[UIColor clearColor]];
    
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    [self addSubview:bgView];
    [bgView setBackgroundColor:[UIColor clearColor]];
    [bgView bk_whenTapped:^{
        [self removeFromSuperview];

    }];
    
    float width = 70;
    float height = 40;
    _boxView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width-85,68, width,height*self.btns.count+5)];
    [_boxView setBackgroundColor:[UIColor clearColor]];
    [bgView addSubview:_boxView];
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, 6)];
    image.image=[UIImage imageNamed:@"Ico_progressAddBG"];
    [_boxView addSubview:image];
    
    UIView *centerView=[[UIView alloc]initWithFrame:CGRectMake(0, 6, width, height*self.btns.count)];
    centerView.backgroundColor=MainColor;
    [centerView layer].masksToBounds=YES;
    centerView.layer.cornerRadius=5;
    [_boxView addSubview:centerView];
    for (int i=0; i<self.btns.count; i++) {
    
        UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, height*i, width, height)];
        [centerView addSubview:btnView];
        NSDictionary *dic=[self.btns objectAtIndex:i];
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, height)];
        lab.text=[dic objectForKey:@"title"];
        lab.font=FF(15);
        lab.textAlignment=NSTextAlignmentCenter;
        lab.textColor=[UIColor whiteColor];
        lab.userInteractionEnabled=YES;
        [btnView addSubview:lab];
        
        UITapGestureRecognizer *tapClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnAction:)];
        [btnView addGestureRecognizer:tapClick];
        [(SuspensionView *)btnView setTag:i];
        if (i<self.btns.count-1) {
            UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(10, height-0.5, width-20, 0.5)];
            line.backgroundColor=C(@"e8e7e7");
            [btnView addSubview:line];
        }
        
    }
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];

}



- (void) btnAction:(UIGestureRecognizer *) button{
    NSInteger tag = button.view.tag;
    if (self.action) {
        [self hideBox];
        self.action(self, tag);
    }
}
- (void) hideBox{
    [self removeFromSuperview];
}

@end
